<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\usermodule\controllers;

use app\models\KuaidisendRecorder;
use app\models\OrderRecorder;

class OrderController extends BaseController{

    public function actionList(){
        $post = \Yii::$app->request->post('page');
        $page = intval($post['page'])-1;
        $type = intval($post['type']);
        $pagesize = \Yii::$app->params['pagesize'];
        $offset = $page*$pagesize;
        $list = OrderRecorder::find()->where('user_id='.$this->user->id);
        switch($type){
            case 0://全部
                break;
            case 1://待付款
                $list = $list->andWhere('is_pay=0');
                break;
            case 2://待发货
                $list = $list->andWhere('is_send=0')->andWhere('is_pay=1');
                break;
            case 3://待收货
                $list = $list->andWhere('is_get=0')->andWhere('is_send=1')->andWhere('is_pay=1');
                break;
            case 4://已完成
                $list = $list->andWhere('is_get=1')->andWhere('is_send=1')->andWhere('is_pay=1');
                break;
        }
        $list = $list->select(['id','order_name','order_content','addtime','status','is_pay','total_price','is_send','is_get','is_end','is_rate'])
        ->orderBy('addtime DESC')->limit($pagesize)->offset($offset)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $list[$k]['order_content'] = json_decode($v['order_content'],true);
        }
        if(count($list)>0){
            $result = ['result'=>1,'data'=>$list,"offset"=>$offset];
        }else{
            $result = ['result'=>2];
        }
        return json_encode($result,JSON_NUMERIC_CHECK);
    }

    public function actionSend(){
        $id = \Yii::$app->request->post('id');

        $model = OrderRecorder::find()->where('id=:id',[':id' => $id])->andWhere('user_id='.$this->user->id)->one();
        if($model->status){
            $result = ['result'=>0,'data'=>'订单已经取消'];
            return json_encode($result);
        }
        if(!$model->is_pay){
            $result = ['result'=>0,'data'=>'订单还未支付'];
            return json_encode($result);
        }
        $arr['id'] = $model->id;
        $arr['k_order'] = $model->k_order;
        $k_model = KuaidisendRecorder::find()->where('id=:id',[':id' => $model->k_name])->one();
        $arr['k_name'] = empty($k_model)?"未知":$k_model->k_name;
        $arr['k_type'] = empty($model->k_order)?0:1;
        $result = ['result'=>1,'data'=>$arr];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }

    public function actionGet(){
        $id = \Yii::$app->request->post('id');
        $model = OrderRecorder::find()->where('id=:id',[':id' => $id])->andWhere('user_id='.$this->user->id)->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关订单，请重新操作'];
            return json_encode($result);
        }
        if($model->is_get){
            $result = ['result'=>0,'data'=>'该订单已确认收货'];
            return json_encode($result);
        }
        if(!$model->is_send){
            $result = ['result'=>0,'data'=>'该订单还未发货'];
            return json_encode($result);
        }
        $model->is_get=1;
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


}
